<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/images/avartar.png" alt />
      </div>
      <!-- 输入信息区域 -->
      <el-form class="login_info_box" :model="ruleForm" :rules="rules" ref="ruleForm">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-s-custom" placeholder="请输入用户名" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            v-model="ruleForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登陆按钮 -->
        <el-form-item class="login_btn_box">
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'hairuil',
        password: '123456'
      },
      // 表单验证规则 prop绑定
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 提交表单
    submitForm(ruleForm) {
      /**
       *@param valid 是否通过校验 boolean
       *@param isVerifyField 没有通过校验的字段
       */
      this.$refs[ruleForm].validate(async (valid, isVerifyField) => {
        console.log(isVerifyField) // 未通过校验的数组对象

        if (!valid) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        console.log({
          username: this.ruleForm.username,
          password: this.ruleForm.password
        })
        // 登陆请求
        // const { data: res } = await this.$http.post('login', this.ruleForm)
        // console.log(res)

        this.ruleForm.username = this.ruleForm.password = ''

        // 存储token 缓存登陆状态
        const token = 'admin'
        window.sessionStorage.setItem('token', token)
        this.$router.push('/home')
        // if (valid) {
        // // 校验通过
        //   alert('提交成功!')
        //   console.log({ username: this.ruleForm.username, password: this.ruleForm.password })
        //   this.ruleForm.username = this.ruleForm.password = ''
        // } else {
        //   // 校验不通过
        //   console.log('error submit!!')
        //   return false
        // }
      })
    },
    // 重置表单
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2d4d6d;
  .login_box {
    height: 300px;
    width: 450px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    background-color: #fff;
    box-shadow: 0 0 10px #ccc;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ccc;
    }
  }
  .login_info_box {
    position: absolute;
    bottom: 0;
    padding: 0 10px;
    box-sizing: border-box;
    width: 100%;
  }
  .login_btn_box {
    text-align: right;
  }
}
</style>
